package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
//@Transactional mongodb的事务必须是复制集的方式
public class TravelCommentServiceImpl implements ITravelCommentService{
    @Autowired
    private TravelCommentRepository repository;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private ITravelCommentService travelCommentService;


    @Override
    public void save(TravelComment travelComment) {
        travelComment.setId(null);//把id设置为null
        travelComment.setCreateTime(new Date());
        //这里是需要为关联的评论
        String rid = travelComment.getRefComment().getId();
        if (StringUtils.hasLength(rid)){
            //回复评论
            TravelComment refcomment = this.get(rid);
            travelComment.setRefComment(refcomment);
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE);
        }

        repository.save(travelComment);//存入travelComment对象

    }

    @Override
    public void update(TravelComment travelComment) {
        repository.save(travelComment);//编辑和保存是一样的,更新之后则要保存travelComment对象

    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);

    }

    @Override
    public TravelComment get(String id) {
        //orEls,查询id,存在就返回,不承诺在就返回null
        return repository.findById(id).orElse(null);

    }

    @Override
    public List<TravelComment> list() {
        //查询所有
        return repository.findAll();
    }

    @Override
    public Page<TravelComment> query(TravelCommentQuery qo) {

        //分页查询
        //查询对象
        Query query = new Query();
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template,TravelComment.class,query,pageable);

    }

    @Override
    public void saveOrUpdate(TravelComment travelComment) {

        if (StringUtils.hasLength(travelComment.getId())){
            //update
            //特殊案例,页面更新的数据,刚好跟集合中字段个数一致
            //所以这种等价于全量的更新
            this.update(travelComment);
        }else {
            //save
            this.save(travelComment);
        }
    }

    /**
     * 游记评论页面的显示
     * @param travelId
     * @return
     */
    @Override
    public List<TravelComment> queryByTravelId(String travelId) {

        return repository.findAllByTravelId(travelId);
    }
}
